<?php include_once('inc/header.php');

if($connected AND !isset($_GET['code']))
{
	echo'
	<!--page--> 
	<div id="banner">
		<div class="container intro_wrapper">
			<div class="inner_content">
			<div class="col-md-3">
				<h1>ACCOUNT MANAGEMENT</h1>
				</div>
			<div class="col-md-9">
					<h4 style="text-transform:uppercase;">Logged as '. $user->firstname . " " . $user->surname .'</h4>
					<h1 class="animated fadeinup" style="font-size: 3.5em;">';
			
			if(isset($_GET['sub']))
			{
				$sub = $_GET['sub'];
				
				switch($sub)
				{
					case "products":
					{
						echo 'MY PRODUCTS';
					}
					break;
					
					case "orders":
					{
						echo'MY ORDERS';
					}
					break;
					
					case "upgrade":
					{
						echo'UPGRADE MY ACCOUNT';
					}
					break;
					
					case "inbox":	
					{
						echo'MY INBOX';
					}
					break;
				}
			}
			else 
			{
				if(isset($_GET['order']))
				{
					if(isUsersOrder($db, $user, $_GET['order']))
					{
						$order = new Order($db, $_GET['order']);
						$isUsersOrder = true;
						echo date_format(new Datetime($order->order_date), 'l jS \of F Y').'\'s order';
					}
					else
					{
						echo'Wrong target';
						$isUsersOrder = false;
					}
				}
				else
				echo'SUMMARY';
			}
			
			echo'
				
				</h1>
				</div>
					</div>
				</div>
			</div>
			
	<div class="container wrapper">
		<div class="inner_content">
	
	<!-- sidebar -->
		<div class="row pad30">'; 
		
	$user->displaysidemenu($db); echo'
	
	<div class="col-md-9">';
	
	if(getUnreadAmount($db, $user)!=0)
	{
		echo '<h2>Notifications</h2>
		<div class="alert alert-info"><span> You have <span class="label label-primary">'. getUnreadAmount($db, $user) .'</span> unread message </span> <a href="">Check your inbox</a></div>';
	}
		
	/* Display of the body */
	
	echo '<div id="containerWrapper">';
	
	if(!isset($_GET['sub']) && !isset($_GET['order'])) // We just display the user's profile
	{
		echo'<h2>Your informations</h2>';
		
		echo'<ul id="myTab" class="nav nav-tabs">
				<li class="active"><a href="#one" data-toggle="tab">View</a></li>
				<li class=""><a href="#two" data-toggle="tab">Edit</a></li>
			 </ul>';
		$one="active in";
		$two="fade";
		
	 
		echo'
		<div id="myTabContent" class="tab-content">
		<div class="tab-pane fade '.$one.'" id="one">
			<p>'; 
			$user->getInfo(); 
		echo'</p>
		</div>

		<div class="tab-pane '.$two.'" id="two">
		  <p>'; 
		  $user->editInfo($db); 
		  echo' </p>
		</div>';
	}
	
	if (isset($_GET['sub']))
	{
		if($_GET['sub'] == "products") // We display the PRODUCTS that the user sells
		{
			$ListProducts = $user->getProductsOnSale($db);
			$nbProductsOnSale = count($ListProducts);	
			$maxLength = 50; // Maximum length for each row of the product's name
			
			if($nbProductsOnSale)
			{
				/* Variable handling the pagination system */
				$cPage = 1; // Current page
				$perPage = 5; // Quantity of displayed products per page
				$nbPages = ceil($nbProductsOnSale/$perPage); // Quantity of all pages
				
				if (isset($_POST['page']) && ($_POST['page'])>0 && ($_POST['page'])<= $nbPages) // We try to retrieve the current page if it has change
				{
					$cPage = htmlspecialchars($_POST['page']);
				}
			
				$firstProduct = ($cPage-1)*$perPage;
				
				echo'
				<table class="table table-striped">
					 <thead>
						 <tr>
							<th>N°</th>
							<th>Title</th>
							<th>Quantity</th>
							<th>Price</th>
							<th>Date of sale</th>
							<th></th>
						</tr>
					</thead>
					<tbody>';
				
				for($i=0;$i<$nbProductsOnSale;$i++)
				{
					$ProductsOnSale[] = new Product($db, $ListProducts[$i]);
				}
				
				for($i=$firstProduct; $i < ($firstProduct+$perPage); $i++)
				{
					if(isset($ProductsOnSale[$i]))
					{
						$name = wordwrap($ProductsOnSale[$i]->name, $maxLength, "</br>", true);
						echo'
							<tr>
								<td style="vertical-align:middle">'.($i+1).'</td>
								<td style="vertical-align:middle;max-width:20%;">'.$name.'</td> 
								<td style="vertical-align:middle">'.$ProductsOnSale[$i]->stock.'</td>
								<td style="vertical-align:middle">'.($ProductsOnSale[$i]->price).' KR</td>
								<td style="vertical-align:middle">'.($ProductsOnSale[$i]->pr_date).'</td>
								<td style="vertical-align:middle"><a class="product" href="show_product.php?pr='.($ProductsOnSale[$i]->id).'"><button type="button" class="btn btn-info">See details <i class="fa fa-search-plus icon-large"></i></button></a></td>
							</tr>
						';
					}
				}
				
				echo' </tbody>
							  </table>';
							  
				/* Displaying the pagination system */	
				
				echo '<!-- //portfolio_block -->   
				<div class="holder">
				
					<a class="page" id="1" href="account.php?sub=products">← first</a>';
					
					if($cPage>2)
					{
						echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
					}
					
					if($cPage>1)
					{
						echo '<a class="page" id="'.($cPage-1).'" href="account.php?sub=products">'.($cPage-1).'</a>';
					}
					
					echo '<a class="jp-current">'.($cPage).'</a>';
					
					if($cPage < $nbPages)
					{
						echo '<a class="page" id="'.($cPage+1).'" href="account.php?sub=products">'.($cPage+1).'</a>';
					}
					
					if($cPage<($nbPages-1))
					{
						echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
					}
					echo'<a class="page" id="'.$nbPages.'" href="account.php?sub=products">last →</a>
				</div>';
			}
			else // The user has put no product on sell for the now
			{
				echo '<legend>No product on sale for the now! <i class="fa fa-bullhorn icon-large"></i></legend>
					  </br><a style="font-size:1.3em" href="sell_yours.php">Let\'s sell some!</a>';
			}
		}
		

		
		else if($_GET['sub'] == "orders") // The user wants to see the ORDERS he made
		{
			/* Variables handling the orders as object*/
			$nbOrders = count($user->orders);
			$Quantity = array();
			$Orders = array();
			
			if($nbOrders) // If the user has already make orders
			{
					/* Variable handling the pagination system */
				$cPage = 1; // Current page
				$perPage = 5; // Quantity of displayed products per page
				$nbPages = ceil($nbOrders/$perPage); // Quantity of all pages
				
				if (isset($_POST['page']) && ($_POST['page'])>0 && ($_POST['page'])<= $nbPages) // We try to retrieve the current page if it has change
				{
					$cPage = htmlspecialchars($_POST['page']);
				}
			
				$firstProduct = ($cPage-1)*$perPage;
				
				echo'
				<table class="table table-striped">
					 <thead>
						 <tr>
							<th>N°</th>
							<th>Date</th>
							<th>Quantity</th>
							<th>Price</th>
							<th></th>
						</tr>
					</thead>
					<tbody>';
					
				for($i=0; $i < ($nbOrders); $i++)
				{
					$Orders[$i] = new Order($db, $user->orders[$i]);
				}
				
				for($i=$firstProduct; $i < ($firstProduct+$perPage); $i++)
				{
					if(isset($Orders[$i]))
					{
						echo'
							<tr>
								<td style="vertical-align:middle">'.($i+1).'</td>
								<td style="vertical-align:middle">'.($Orders[$i]->order_date).'</td>
								<td style="vertical-align:middle">'.$Orders[$i]->getTotalQty().'</td>
								<td style="vertical-align:middle">'.($Orders[$i]->price).' KR</td>
								<td style="vertical-align:middle"><a class="order" href="account.php?order='.($Orders[$i]->id).'"><button type="button" class="btn btn-info">Details <i class="fa fa-search-plus icon-large"></i></button></a></td>
							</tr>
						';
					}
				}
				
				echo' </tbody>
							  </table>';
							  
				/* Displaying the pagination system */	
				
				echo '<!-- //portfolio_block -->   
				<div class="holder">
				
				<a class="page" id="1" href="account.php?sub=orders">← first</a>';
				
				if($cPage>2)
				{
					echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
				}
				
				if($cPage>1)
				{
					echo '<a class="page" id="'.($cPage-1).'" href="account.php?sub=orders">'.($cPage-1).'</a>';
				}
				
				echo '<a class="jp-current">'.($cPage).'</a>';
				
				if($cPage < $nbPages)
				{
					echo '<a class="page" id="'.($cPage+1).'" href="account.php?sub=orders">'.($cPage+1).'</a>';
				}
				
				if($cPage<($nbPages-1))
				{
					echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
				}
				echo'<a class="page" id="'.$nbPages.'" href="account.php?sub=orders">last →</a>
				</div>';
			}
			else // The user has make no order
			{
				echo '<legend>No order operated for the now! <i class="fa fa-bullhorn icon-large"></i></legend>
					  </br><a style="font-size:1.3em" href="show_products.php">Let\'s go back shopping</a> OR <a style="font-size:1.3em" href="cart.php">check your cart!</a>';
			}
		}
		
		else if($_GET['sub'] == "inbox")
		{
			include_once('inc/user.inbox.php');
		}
	}
	
	if(isset($_GET['order']))
	{
			/* The user wants to display a specific order */
		if($isUsersOrder)
		{
			$nbProducts = count($order->Products);
			$cPage = 1; // Current page
			$perPage = 5; // Quantity of displayed products per page
			$nbPages = ceil($nbProducts/$perPage); // Quantity of all pages
			$maxLength = 50;
			
			for($i=0;$i<$nbProducts;$i++)
			{
				$ProductsFromOrder[] = new Product($db, $order->Products[$i]);
			}
			
			if (isset($_POST['page']) && ($_POST['page'])>0 && ($_POST['page'])<= $nbPages) // We try to retrieve the current page if it has change
			{
				$cPage = htmlspecialchars($_POST['page']);
			}
		
			$firstProduct = ($cPage-1)*$perPage;
		
			echo'
			<table class="table table-striped">
				 <thead>
					 <tr>
						<th>N°</th>
						<th>Product</th>
						<th>Quantity</th>
						<th>Total Price</th>
						<th></th>
					</tr>
				</thead>
				<tbody>';
				
			for($i=$firstProduct; $i < ($firstProduct+$perPage); $i++)
			{
				if(isset($ProductsFromOrder[$i]))
				{
					$name = wordwrap($ProductsFromOrder[$i]->name, $maxLength, "</br>", true);
					
					echo'
						<tr>
							<td style="vertical-align:middle">'.($i+1).'</td>
							<td style="vertical-align:middle;max-width:20%;">'.$name.'</td> 
							<td style="vertical-align:middle">'.($order->Quantity[$i]).'</td>
							<td style="vertical-align:middle">'.($ProductsFromOrder[$i]->price*$order->Quantity[$i]).' KR</td>
							<td style="vertical-align:middle"><a class="product" href="show_product.php?pr='.($ProductsFromOrder[$i]->id).'"><button type="button" class="btn btn-info">Details <i class="fa fa-search-plus icon-large"></i></button></a></td>
						</tr>
					';
				}
			}
			
			echo' </tbody>
							  </table>';
			
			/* Displaying the pagination system */	
		
			echo '<!-- //portfolio_block -->   
			<div class="holder">
		
			<a class="page" id="1" href="account.php?order='.$order->id.'">← first</a>';
			
			if($cPage>2)
			{
				echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
			}
			
			if($cPage>1)
			{
				echo '<a class="page" id="'.($cPage-1).'" href="account.php?order='.$order->id.'">'.($cPage-1).'</a>';
			}
			
			echo '<a class="jp-current">'.($cPage).'</a>';
			
			if($cPage < $nbPages)
			{
				echo '<a class="page" id="'.($cPage+1).'" href="account.php?order='.$order->id.'">'.($cPage+1).'</a>';
			}
			
			if($cPage<($nbPages-1))
			{
				echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
			}
			
			echo'<a class="page" id="'.$nbPages.'" href="account.php?order='.$order->id.'">last →</a>
			</div>';
			
		}
		
		else// If the orders doesn't exists or it it's not the user's one
		{
			echo '<label>The order you tried to retrieve is not yours or is not displayable now <i class="fa fa-times-circle icon-large"></i>
			</br><span class="hue">Please try again!<span></label>';
		}
	}
	
	echo'</div>
		</div>	
		</div>
			</div>
				</div>
					</div>
						<div class="pad45 hidden-md hidden-lg"></div>
					<!--//page-->					
	';	
}

if(!$connected AND !isset($_POST['op']) AND !isset($_GET['code']))
{
$captcha=randomString(7);
echo'

<!--page--> 
	<div id="banner">
		<div class="container intro_wrapper">
			<div class="inner_content">
				<h1>HI, VISITOR</h1>
					<h1 class="title animated fadeinup">LOG IN OR REGISTER</h1>
						
					</div>
				</div>
			</div>
		<div class="container wrapper">
	<div class="inner_content">
	
	<!-- sidebar -->
		<div class="row pad30">
			<div class="col-md-4">
			 <h2><span style="text-transform: uppercase;">I already have an account</span></h4>
				<p>Hello ! Nice to see you, dear member. I forgot your name and password, can you just refresh my memory ?</p>
				
				<form method="post" action="account.php" >
			<p class="form_info">
					E-mail address 
			<span class="required"></span>
				</p>
				<input type="email" value="" name="log_email" style="width:100%"></input><br></br>
				<p class="form_info">Password</p>
				<input type="password" value="" name="log_password" style="width:100%"></input>

							<br></br>
					 <div class="alert alert-info">
				  <strong>Info:</strong> This website uses cookies to keep you logged in.
				</div>
					 <input class="btn btn-primary" type="submit" value="Log me in !" style="width:100%"/>
					
					</form>	
					
					</div>
			
			<div class="col-md-8">
			<!-- sidebar -->
			
			<!-- /SLIDER set height and width of your images in the script at the bottom of page -->
				<div class="myslider1">
					<img src="img/large/index_slider1.jpg" alt="" >
					<img src="img/large/index_slider2.jpg" alt="" >
					<img src="img/large/index_slider3.jpg" alt="" >
				</div>
			
			<h2><span style="text-transform: uppercase;">I just came in. Let me register!</span></h2>
			
			<p>
				Great ! A new member ! Just fill out this form and... welcome to the community, sir ! 
			</p>
			
			<p>
				<form novalidate="novalidate" method="post" action="account.php" id="register-form" class="register-form"/>
					
					<p class="form_info" style="font-size:1em;">
					Log-in information
					<span class="required" >*</span>
						</p>
					<input type="email" name="email" id="email" placeholder="Enter your email..."></input>
					<input name="password" id="password" placeholder="Enter your desired password..." type="password"/><br><br/>
					
					
					<p class="form_info" style="font-size:1em;">
					Personal information
					<span class="required" >*</span>
						</p>
						
					<select name="gender" id="gender" placeholder="Gender" style="width:230px;">
					<option value="male">Mr.</option>
					<option value="female">Mrs.</option>
					</select>
					<input name="firstname" id="firstname" placeholder="First name" type="text"/>
					<input name="surname" id="surname" placeholder="Surname" type="text"/>
					<input name="phone" id="phone" placeholder="Phone number..." type="text"/>
					<input name="birth" id="birth" placeholder="Date of birth (YYYY-MM-DD)..." type="text"/>
					<textarea name="address" id="address" placeholder="Address"></textarea><br></br>
					
					<p class="form_info" style="font-size:1em;">
					Captcha / Anti-bot security
					<span class="required" >*</span>
						</p>
					<canvas id="captchaR" width="230" height="50"></canvas>
					<script>
					var ctxR = document.getElementById(\'captchaR\').getContext(\'2d\');
					var kittyR = new Image();
					kittyR.src = \'img/captchagrid.png\';
					kittyR.onload = function(){
					  ctxR.drawImage(this, 0,0,this.width, this.height);
					  ctxR.font         = \'45px Tequillasunrise\';
					  ctxR.fillStyle = \'#333399\';
					  ctxR.textBaseline = \'top\';
					  ctxR.fillText  (\''.$captcha.'\', 10, 2);
					};
					</script><br/>
					<input name="captcha" id="captcha" placeholder="Retype captcha..." type="text" style="width:230px;"/><br><br/>
					<input type="hidden" name="cc" value="'.sha1($captcha).'" />
					<p><label> I have read and accept <a>the terms of condition</a>.<br/> We won\'t share your personal data with third party companies.</label></p>
					<input type="hidden" name="op" value="register" />
					<input type="hidden" name="active" value="notyet" />
					<input value="Register!" class="btn btn-primary" type="submit">
				</form>
			</p>
			
			
		</div>
			</div>
				</div>
					</div>
						<div class="pad45 hidden-md hidden-lg"></div>
					<!--//page-->';

}


if(isset($_POST['op']) AND $_POST['op']=="register" AND !isset($_GET['code']))
{
$captcha=randomString(7);
echo'

<!--page--> 
	<div id="banner">
		<div class="container intro_wrapper">
			<div class="inner_content">
				<h1>HI, VISITOR</h1>
					<h1 class="title animated fadeinup">LOG IN OR REGISTER</h1>
						
					</div>
				</div>
			</div>
		<div class="container wrapper">
	<div class="inner_content">
	
	<!-- sidebar -->
		<div class="row pad30">
			
			<div class="col-md-12">
			';
			
			check_user_data($db, $_POST['email'], $_POST['password'], $_POST['gender'], $_POST['firstname'], $_POST['surname'], $_POST['phone'], $_POST['birth'], $_POST['address'], 'member', $_POST['captcha'], $_POST['cc'], $transport, $website_url);
			
			echo'
			
		</div>
			</div>
				</div>
					</div>
						<div class="pad45 hidden-md hidden-lg"></div>
					<!--//page-->';
}

if(isset($_GET['op']) AND $_GET['op']=="activate" AND isset($_GET['code']))
{
echo'

<!--page--> 
	<div id="banner">
		<div class="container intro_wrapper">
			<div class="inner_content">
				<h1>HI, VISITOR</h1>
					<h1 class="title animated fadeinup">LOG IN OR REGISTER</h1>
						
					</div>
				</div>
			</div>
		<div class="container wrapper">
	<div class="inner_content">
	
	<!-- sidebar -->
		<div class="row pad30">
			
			<div class="col-md-12">
			';
			$activation=activate($db, $_GET['code']);
			if($activation==false)
			{
				echo'Error. Activation URL seems invalid.';
			}
			else
			{
				$activated_user=new User($db, $activation);
				echo 'Your account has been activated, '.$activated_user->firstname.' ! ';
			}
			
			echo'
			
		</div>
			</div>
				</div>
					</div>
						<div class="pad45 hidden-md hidden-lg"></div>
					<!--//page-->';

}


	include_once('inc/footer.php'); ?>